package com.gopay.solution.domain.bo.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.common.domain.view.config.View;
import com.gopay.common.domain.view.config.ViewSolutionConfig;
import com.gopay.common.domain.view.config.ViewSolutionTransView;
import com.gopay.common.util.xml.constants.XmlMsgType;
import com.gopay.common.util.xml.tools.XmlToolsService;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsUIConfigDtl;
import com.gopay.solution.domain.vo.Event;
import com.gopay.solution.domain.vo.Result;

/**
 * 
 * @SsUIConfigSoluRuleExecutor.java
 * @author liujy
 * @2013-5-7 上午9:24:13 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssUIConfigSoluRuleExecutor")
public class SsUIConfigSoluRuleExecutor extends AbstractSolutionRuleExecutor {

    @Resource(name = "xmlToolsService")
    private XmlToolsService xmlToolsService;

    @Override
    public Result execute(List<SolutionRuleDtl> rules, Event event) {

        

        Result res = new Result();
        res.setSolved(false);
        res.setFailedReason("");
        res.setDetail(null);

        String intTxnCd = (String) event.getDetail();
        ViewSolutionTransView view = null;
        
        if (null == rules || rules.isEmpty()) {
            res.setDetail(view);
            return res;
        }

        SolutionRuleDtl rule = rules.get(0);
        
       
        
        SsUIConfigDtl dtl = (SsUIConfigDtl) rule;

        if (null != dtl) {

            ViewSolutionConfig config = (ViewSolutionConfig) xmlToolsService.xml2j(
                    XmlMsgType.VIEW_CASHIER_UI_PAYCHANNEL_TYPE, dtl.getConfigExp());
            if (null != config) {
                List<View> views = config.getViews();
                for (int i = 0, size = null == views ? 0 : views.size(); i < size; i++) {
                    view = (ViewSolutionTransView) views.get(i);
                    if (null != view && view.getIntTxnCd().equals(intTxnCd)) {
                        break;
                    } else {
                        view = null;
                    }
                }
            }
        }

        
        res.setDetail(view);
        res.setSolved(true);

        return res;
    }
}